package cn.bearspark.framework.es.starter.config;

import org.apache.http.HttpHost;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * es 的高级客户端的配置类
 *
 * @author f
 */
@Configuration
@ConfigurationProperties(prefix = "bs.es")
public class RestHighLevelClientConfig {

    /**
     * es 的 ip 地址
     */
    private String host;

    /**
     * es 的端口
     */
    private Integer port;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        // 禁用 es 的查询日志
        ((LoggerContext) LogManager.getContext(false))
                .getConfiguration()
                .getLoggerConfig("org.elasticsearch.client.RestHighLevelClient")
                .setLevel(Level.ERROR);
        return new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }
}
